﻿namespace Huayou.UI.WebControls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ToolboxBitmap(typeof(DataGridTable), "Huayou.UI.WebControls.DataGridTable.bmp"), DefaultProperty("Text"), ToolboxData("<{0}:DataGridTable runat=server></{0}:DataGridTable>")]
    public class DataGridTable : DataGrid, INamingContainer, IPostBackEventHandler
    {
        private LinkButton endPage = new LinkButton();
        private Huayou.UI.WebControls.Panel myPanel = new Huayou.UI.WebControls.Panel();
        private LinkButton nextPage = new LinkButton();
        private DropDownList numPage = new DropDownList();
        private string pageCSS = "scrollButton";
        private LinkButton prevPage = new LinkButton();
        private string sEndPage = "Last";
        private string sNextPage = "Next";
        private string sPrevPage = "Prev";
        private string sStartPage = "First";
        private LinkButton startPage = new LinkButton();

        protected override bool OnBubbleEvent(object source, EventArgs e)
        {
            return base.OnBubbleEvent(source, e);
        }

        protected override void OnInit(EventArgs e)
        {
            this.EnableViewState = true;
            if ((base.CssClass == null) || base.CssClass.Trim().Equals(""))
            {
                this.CssClass = "tableGrid";
            }
            if ((base.HeaderStyle.CssClass == null) || base.HeaderStyle.CssClass.Trim().Equals(""))
            {
                this.HeaderStyle.CssClass = "gridHeader";
            }
            if ((base.FooterStyle.CssClass == null) || base.FooterStyle.CssClass.Trim().Equals(""))
            {
                this.FooterStyle.CssClass = "gridFooter";
            }
            if ((base.PagerStyle.CssClass == null) || base.PagerStyle.CssClass.Trim().Equals(""))
            {
                this.PagerStyle.CssClass = "gridPager";
            }
            if ((base.ItemStyle.CssClass == null) || base.ItemStyle.CssClass.Trim().Equals(""))
            {
                this.ItemStyle.CssClass = "gridItem";
            }
            if ((base.AlternatingItemStyle.CssClass == null) || base.AlternatingItemStyle.CssClass.Trim().Equals(""))
            {
                this.AlternatingItemStyle.CssClass = "gridAltItem";
            }
            if ((base.EditItemStyle.CssClass == null) || base.EditItemStyle.CssClass.Trim().Equals(""))
            {
                this.EditItemStyle.CssClass = "gridEditItem";
            }
            if ((base.SelectedItemStyle.CssClass == null) || base.SelectedItemStyle.CssClass.Trim().Equals(""))
            {
                this.SelectedItemStyle.CssClass = "gridSelectedItem";
            }
            base.OnInit(e);
        }

        protected override void OnItemCommand(DataGridCommandEventArgs e)
        {
            base.OnItemCommand(e);
        }

        protected override void OnItemDataBound(DataGridItemEventArgs e)
        {
            base.OnItemDataBound(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            this.Page.GetPostBackEventReference(this);
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            int newPageIndex = 0;
            if (this.PagerStyle.Position.Equals(PagerPosition.Bottom))
            {
                base.CurrentPageIndex = Convert.ToInt32(this.Page.Request.Params[this.UniqueID + ":numPageBottom"]) - 1;
            }
            else
            {
                base.CurrentPageIndex = Convert.ToInt32(this.Page.Request.Params[this.UniqueID + ":numPageTop"]) - 1;
            }
            if ("prevPage".Equals(eventArgument))
            {
                newPageIndex = base.CurrentPageIndex - 1;
            }
            else if ("nextPage".Equals(eventArgument))
            {
                newPageIndex = base.CurrentPageIndex + 1;
            }
            else if (eventArgument.StartsWith("endPage"))
            {
                newPageIndex = Convert.ToInt32(eventArgument.Substring(7));
            }
            else if ("numPageTop".Equals(eventArgument))
            {
                newPageIndex = Convert.ToInt32(this.Page.Request.Params[this.UniqueID + ":numPageTop"]) - 1;
            }
            else if ("numPageBottom".Equals(eventArgument))
            {
                newPageIndex = Convert.ToInt32(this.Page.Request.Params[this.UniqueID + ":numPageBottom"]) - 1;
            }
            this.OnPageIndexChanged(new DataGridPageChangedEventArgs(this, newPageIndex));
        }

        protected override void Render(HtmlTextWriter output)
        {
            if (this.IsAllowPaging)
            {
                this.AllowPaging = true;
                this.PagerStyle.Visible = false;
                this.myPanel.CopyBaseAttributes(this);
                base.Style.Remove("POSITION");
                base.Style.Remove("Z-INDEX");
                this.myPanel.RenderBeginTag(output);
                Table table = new Table();
                table.CopyBaseAttributes(this);
                table.BackColor = this.PagerStyle.BackColor.Equals(Color.Empty) ? this.BackColor : this.PagerStyle.BackColor;
                table.BackImageUrl = this.BackImageUrl;
                table.BorderColor = this.PagerStyle.BorderColor.IsEmpty ? this.BorderColor : this.PagerStyle.BorderColor;
                table.BorderStyle = this.PagerStyle.BorderStyle;
                table.BorderWidth = Unit.Point(0);
                table.HorizontalAlign = this.HorizontalAlign;
                table.Width = this.Width;
                table.CellPadding = this.CellPadding;
                table.CellSpacing = this.CellSpacing;
                table.GridLines = this.GridLines;
                table.Font.Size = FontUnit.Point(9);
                TableRow child = new TableRow();
                table.Controls.Add(child);
                TableCell cell = new TableCell();
                this.startPage.CssClass = this.pageCSS;
                this.startPage.Text = this.sStartPage;
                this.prevPage.CssClass = this.pageCSS;
                this.prevPage.Text = this.sPrevPage;
                this.nextPage.CssClass = this.pageCSS;
                this.nextPage.Text = this.sNextPage;
                this.endPage.CssClass = this.pageCSS;
                this.endPage.Text = this.sEndPage;
                this.numPage.CssClass = this.pageCSS;
                child.Controls.Add(cell);
                cell.Attributes["style"] = "BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none";
                cell.Controls.Add(new LiteralControl(string.Concat(new object[] { "&nbsp;&nbsp;Total<font color=red>", this.MaxRecord, "</font>/<font color=red>", this.PageSize, "</font>Items" })));
                cell.Controls.Add(new LiteralControl(string.Concat(new object[] { "&nbsp;<font color=red>", base.CurrentPageIndex + 1, "</font> Page/<font color=red>", base.PageCount, "</font> Pages" })));
                cell.Controls.Add(new LiteralControl("</td><td  style=\"BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none\">"));
                if (base.CurrentPageIndex != 0)
                {
                    this.startPage.Attributes["href"] = "javascript:" + this.Page.GetPostBackEventReference(this, "startPage");
                    this.prevPage.Attributes["href"] = "javascript:" + this.Page.GetPostBackEventReference(this, "prevPage");
                    cell.Controls.Add(this.startPage);
                    cell.Controls.Add(new LiteralControl("&nbsp;"));
                    cell.Controls.Add(this.prevPage);
                    cell.Controls.Add(new LiteralControl("&nbsp;"));
                }
                else
                {
                    cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
                }
                if (base.CurrentPageIndex < (base.PageCount - 1))
                {
                    this.nextPage.Attributes["href"] = "javascript:" + this.Page.GetPostBackEventReference(this, "nextPage");
                    this.endPage.Attributes["href"] = "javascript:" + this.Page.GetPostBackEventReference(this, "endPage" + (base.PageCount - 1));
                    cell.Controls.Add(this.nextPage);
                    cell.Controls.Add(new LiteralControl("&nbsp;"));
                    cell.Controls.Add(this.endPage);
                    cell.Controls.Add(new LiteralControl("&nbsp;"));
                }
                else
                {
                    cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
                }
                for (int i = 0; i < base.PageCount; i++)
                {
                    int num2 = i + 1;
                    ListItem item = new ListItem(num2.ToString());
                    if (i == base.CurrentPageIndex)
                    {
                        item.Selected = true;
                    }
                    this.numPage.Items.Add(item);
                }
                cell.Controls.Add(new LiteralControl("</td><td align=right style=\"BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none\"> Go"));
                cell.Controls.Add(this.numPage);
                cell.Controls.Add(new LiteralControl("page"));
                if ((this.PagerStyle.Position == PagerPosition.Top) || (this.PagerStyle.Position == PagerPosition.TopAndBottom))
                {
                    this.numPage.ID = this.UniqueID + ":numPageTop";
                    this.numPage.Attributes["onchange"] = this.Page.GetPostBackEventReference(this, "numPageTop");
                    table.RenderControl(output);
                }
                base.Render(output);
                if ((this.PagerStyle.Position == PagerPosition.Bottom) || (this.PagerStyle.Position == PagerPosition.TopAndBottom))
                {
                    this.numPage.ID = this.UniqueID + ":numPageBottom";
                    this.numPage.Attributes["onchange"] = this.Page.GetPostBackEventReference(this, "numPageBottom");
                    table.RenderControl(output);
                }
                this.myPanel.RenderEndTag(output);
            }
            else
            {
                base.Render(output);
            }
            this.EnableViewState = false;
        }

        [DefaultValue("Last"), Description("回到尾页"), Category("翻页"), Bindable(true)]
        public string EndPage
        {
            get
            {
                return this.sEndPage;
            }
            set
            {
                this.sEndPage = value;
            }
        }

        [DefaultValue("True"), Description("自定义分页"), Category("分页"), Bindable(true)]
        public bool IsAllowPaging
        {
            get
            {
                object obj2 = this.ViewState["isAllowPaging"];
                return ((obj2 == null) || Convert.ToBoolean(obj2));
            }
            set
            {
                this.ViewState["isAllowPaging"] = value;
            }
        }

        [Description("总记录数"), Category("分页"), Bindable(true)]
        public int MaxRecord
        {
            get
            {
                object obj2 = this.ViewState["maxRecord"];
                return ((obj2 == null) ? 0 : Convert.ToInt32(obj2));
            }
            set
            {
                this.ViewState["maxRecord"] = value;
            }
        }

        [Description("下一页"), DefaultValue("Next"), Bindable(true), Category("翻页")]
        public string NextPage
        {
            get
            {
                return this.sNextPage;
            }
            set
            {
                this.sNextPage = value;
            }
        }

        [Category("翻页外观"), DefaultValue(""), Description("按钮的CSS"), Bindable(true)]
        public string PageCSS
        {
            get
            {
                return this.pageCSS;
            }
            set
            {
                this.pageCSS = value;
            }
        }

        [Description("上一页"), Category("翻页"), DefaultValue("Prev"), Bindable(true)]
        public string PrevPage
        {
            get
            {
                return this.sPrevPage;
            }
            set
            {
                this.sPrevPage = value;
            }
        }

        [Bindable(true), Description("回到首页"), Category("翻页"), DefaultValue("First")]
        public string StartPage
        {
            get
            {
                return this.sStartPage;
            }
            set
            {
                this.sStartPage = value;
            }
        }
    }
}

